let query = (location.href.split("&")[0]).split("=")[1]; //商品种类id
let query2 = 1;
let title = location.href.split("=")[2]; //导航名
render(query, query2, title);

function render(query, query2, title) {
    // 渲染导航
    function renderNav(res) {
        let navHtml = "";
        for (let i = 0; i < res.length; i++) {
            navHtml += `
            <a href="index.html">首页></a>
            <a href="category.html">全部分类></a>
            <a href="">${res[i].category}></a>
            <a href="" id="button" class="fr">筛选</a>`
        }
        $('#subNav').html(navHtml);
    }
    // 渲染商品列表
    function renderUl(res) {
        let ulHtml = "";
        for (let j = 0; j < res.length; j++) {
            ulHtml += `
            <li>
            <a href="productlist.html?title=${title}&productid=${res[j].productId}">
                <div class="img">${res[j].productImg}</div>
                <div class="words">
                    <div class="title">
                        <h4>${res[j].productName}</h4>
                        <div class="price">${res[j].productPrice}</div>
                    </div>
                    <div class="other">
                        <span class="take_price">${res[j].productQuote}</span>
                        <span>${res[j].productCom}</span>
                    </div>
                </div>
            </a>
            </li>
            `
        }
        $('.goods').html(ulHtml);
    }

    // 导航
    ajax({
        url: "http://chst.vip:1234/api/getcategorybyid",
        data: { categoryid: query },
        success(res) {
            res = res.result;
            renderNav(res)
        }
    })


    // 商品列表

    ajax({
        url: "http://chst.vip:1234/api/getproductlist",
        data: {
            categoryid: query,
            pageid: query2
        },
        success(res) {
            res_ = res.result;
            renderUl(res_);
            // renderPage(res);
        }
    })


    //分页选择器


}



ajax({
    url: "http://chst.vip:1234/api/getproductlist",
    data: {
        categoryid: query,
        pageid: query2
    },
    success(res) {
        /* renderPage(res); */
        let selectHtml = "";
        let pages = Math.ceil(res.totalCount / res.pagesize);
        for (let k = 1; k <= pages; k++) {
            selectHtml += `
            <option class="_page" option_id=${k}>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${k}/${pages}</option>
            `
        }
        $('#changePage #select_').html(selectHtml);

        $('.last').attr('disabled', true)


        console.log(pages);

        $('#changePage .last').click(() => {
            query2--;

            $('.next').attr('disabled', false)
            if (query2 === 1) {
                console.log(111);
                $('.last').attr('disabled', true)
                $('#select_ ._page').eq(query2 - 1).attr("selected", 'true').siblings().attr('selected', false);
            }
            if (query2 > 1) {
                $('.last').attr('disabled', false)
                render(query, query2, title);
                $('#select_ ._page').eq(query2 - 1).attr("selected", 'true').siblings().attr('selected', false);
                // $('#select_ ._page').eq(query2 - 1).attr("selected", true)
            }
            console.log('上一页');
            console.log(query2);
            // page[num].selected=true
        })
        $('#changePage .next').click(() => {
            console.log('下一页');
            $('.last').attr('disabled', false)
            query2++;
            if (query2 === pages) {
                $('.next').attr('disabled', 'true')
            }
            if (query < pages) {

                render(query, query2, title)
                console.log(query2);
                // $('#select_ ._page').eq(query2 - 1).attr("selected", 'true').siblings().attr('selected', false);
                $('#select_ ._page').eq(query2 - 1).attr("selected", 'true').siblings().attr('selected', false);
                query2 = $('#select_').val();
                console.log(query2 + "厉害");
            }
        })
        $('#select_').change(() => {
            console.log($(this).val()); //当前选中的select值
            // query2 = $(this).val();
            query2 = $('#select_  option:selected').attr('option_id');
            console.log(query2);
            $('#main').html = "";
            render(query, query2, title);
            $('#select_ ._page').eq(query2 - 1).attr("selected", 'true').siblings().attr('selected', false);
            console.log("这是第" + query2 + "页");
        })
    }
})